package com.mall.b2c.plugin.search;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;

import com.mall.b2c.domain.goods.GoodsCategoryDomain;
import com.mall.b2c.util.DictionaryUtils;
import com.mall.common.enums.MedicalInsuranceType;
import com.mall.common.hibernate.Finder;
import com.mall.common.plugin.AutoRegisterPlugin;
import com.mall.common.util.UrlUtils;
/**
 * 商品医保类型筛选器
 * @author wangzx
 *
 */
public class MedicalInsuranceTypeSearchFilter{//} extends AutoRegisterPlugin implements IGoodsSearchFilter{

	/*@Override
	public void filter(Finder finder, GoodsCategoryDomain cat, String urlFragment) {
		if(StringUtils.isNotBlank(urlFragment)) {
			if(urlFragment.equals("classAB")) {
				finder.append( " and (g.medicalInsuranceType = :classA or g.medicalInsuranceType = :classB)");
				finder.setParam("classA", MedicalInsuranceType.classA);
				finder.setParam("classB", MedicalInsuranceType.classB);
			} else if(urlFragment.equals("classA")) {
				finder.append( " and g.medicalInsuranceType = :classA");
				finder.setParam("classA", MedicalInsuranceType.classA);
			} else if(urlFragment.equals("classB")) {
				finder.append( " and g.medicalInsuranceType = :classB");
				finder.setParam("classB", MedicalInsuranceType.classB);
			}
		}
	}

	@Override
	public List<SearchSelector> createSelectorList(GoodsCategoryDomain cat,
			String url, String urlFragment, Map<String, String> paramMap,
			String hql, Finder finder) {
		List<SearchSelector> searchSelectors = new ArrayList<SearchSelector>();
		*//**1、当前已选择某个医保类型*//*
		if(StringUtils.isNotBlank(urlFragment)) {
			if(urlFragment.equals("classAB") || urlFragment.equals("classA") || urlFragment.equals("classB")) {
				SearchSelector searchSelector = new SearchSelector();
				if(urlFragment.equals("classAB")) {
					searchSelector.setName("医保类");
				} else if(urlFragment.equals("classA")) {
					searchSelector.setName("医保甲类");
				} else if(urlFragment.equals("classB")) {
					searchSelector.setName("医保乙类");
				}
				searchSelector.setSelected(Boolean.TRUE);
				searchSelector.setUrl(url);
				searchSelectors.add(searchSelector);
				return searchSelectors;
			}
		}
		finder.resetHQL("select g.medicalInsuranceType, count(*) from " + hql + " group by g.medicalInsuranceType");
		List<Object[]> resultList = finder.findHQLList();
		if(!CollectionUtils.isEmpty(resultList)) {
			Long count = 0L;
			SearchSelector searchSelector = null;
			for(Object[] rs : resultList) {
				MedicalInsuranceType medicalInsuranceType = rs[0] != null ? (MedicalInsuranceType)rs[0]  : null;
				if(medicalInsuranceType == null) {
					continue;
				}
				count += (Long)rs[1];
				searchSelector = new SearchSelector();
				searchSelector.setName(DictionaryUtils.getEnumName(medicalInsuranceType));
				searchSelector.setCount((Long)rs[1]);
				searchSelector.setUrl(UrlUtils.addUrlParam(url, getFilterId(), medicalInsuranceType.name(), false));
				searchSelector.setSelected(Boolean.FALSE);
				searchSelectors.add(searchSelector);
			}
			if(count > 0) {
				searchSelector = new SearchSelector();
				searchSelector.setName("医保类");
				searchSelector.setCount(count);
				searchSelector.setUrl(UrlUtils.addUrlParam(url, getFilterId(), "classAB", false));
				searchSelector.setSelected(Boolean.FALSE);
				searchSelectors.add(0, searchSelector);
			}
		}
		return searchSelectors;
	}

	@Override
	public String getFilterId() {
		return "medicalInsuranceType";
	}

	@Override
	public String getSysId() {
		return "medicalInsuranceTypeList";
	}

	@Override
	public String getType() {
		return "goodsSearchFilter";
	}

	@Override
	public String getId() {
		return "medicalInsuranceTypeSearchFilter";
	}

	@Override
	public String getName() {
		return "商品医保类型筛选器";
	}

	@Override
	public String getVersion() {
		return "1.0";
	}

	@Override
	public String getAuthor() {
		return "wangzx";
	}

	@Override
	public void perform(Object... params) {
		
	}

	@Override
	public void register() {
		
	}*/
}
